package edu.zut.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SessionServlet", value = "/session")
public class SessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //session是从请求中获取的
        HttpSession session = request.getSession();
        //判断当前session是否是新的session
        if (session.isNew()){
            System.out.println("这是新的session");
        }else {
            System.out.println("这是旧的session");
        }
        //保存数据
        session.setAttribute("myName", "xueyin");
        //获取数据
        Object myName = session.getAttribute("myName");
        System.out.println("myName = " + myName);
        //移除session中的某个数据
//        session.removeAttribute("");
        //session无效化
//        session.invalidate();
        //session默认是一个会话周期内有效  设置存活期    默认30分钟
        session.setMaxInactiveInterval(24 * 60 * 60);

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
